<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #root{
      border:1px solid red;
    }
  </style>
</head>
<body>
<div id="root"></div>
<ul>
  <li><a href="#/a">/a</a></li>
  <li><a href="#/b">/b</a></li>
  <li>除此之外 浏览器的前进、后退以及执行history.forward、history.back、和history.go 也会触发hashchange事件</li>
  <li>window.location.hash=xxx也行</li>
</ul>
<script>
  window.addEventListener('hashchange',()=>{
    console.log('window.location.hash:',window.location.hash);
    let pathname = window.location.hash.slice(1);//把最前面的那个#删除
    root.innerHTML = pathname;
  });

  //  window.location.hash = '/' ---> https://www.xxx.com/#/ (原地址为https://www.xxx.com,则会加上#/)
  //  window.location.hash = '', 如果原地址没有带#, 则不会加上, 但如果原地址带了#, 也不会去掉
</script>
</body>
</html>
